<?php
/**
 * 购买用户表buyer
 * String id
 * String order_log_id【订单号】
 * String name【用户名】
 * String contact_name【联系姓名】
 * String address【地址】
 * String country【国家】
 * String city【城市】
 * String state【地区】
 * String post_code【邮编】
 * String phone【联系电话】
 * String facebook
 * String twitter
 * String order_id【订单号】
 * String site【网站】
 * String add_time
 */
class App_Model_Buyer extends App_Model_Abstract
{
	public function init()
	{
		parent::init();
		$this->table = App_Db_Table::factory('Buyer');
	}

	public function add($row)
	{
		return $this->table->insert($row);
	}
	
	public function delete($id)
	{
		$db = $this->table->getAdapter();
		$where = $db->quoteInto('id = ?', $id);
	
		return $this->table->delete($where);
	}
	
	public function update($row, $orderid, $site)
	{
		$db = $this->table->getAdapter();
		$where=array();
		$where[] = $db->quoteInto('order_id = ?', $orderid);
		$where[] = $db->quoteInto('site = ?', $site);
		$where=join(' AND ',$where);
	
		return $this->table->update($row, $where);
	}
	
	/**根据订单号，网站等获取购买用户*/
	public function getBuyer($fieldArray)
	{
		$select = $this->table->select();
		if($fieldArray!=null){
			foreach ($fieldArray as $field => $value){
				$select->where("$field = ?",$value);
			}
		}
		return $this->table->fetchAll($select);
	}
	
	public function listAll($orderid,$site,$page=1,$limit=20)
	{
		$select = $this->table->select();
		if(!empty($orderid)){
			$select->where('order_id = ?',$orderid);
		}
		if(!empty($site)){
			$select->where('site = ?',$site);
		}
		
		$offset = ($page - 1) * $limit;
		$select->limit($limit,$offset);
	
		$select->order('id desc');
		return $this->table->fetchAll($select);
	}
	
	public function getTotalNum($orderid,$site,$statu,$starttime,$endtime)
	{
		$where="where 1 ";
	
		if(!empty($orderid)){
			$where.=" and order_id='$orderid'";
		}
		if(!empty($statu)){
			$where.=" and statu='$statu'";
		}
		if(!empty($site)){
			$where.=" and site='$site'";
		}
		if(!empty($starttime)){
			$where.=" and add_time>='".$starttime."'";
		}
		if(!empty($endtime)){
			$where.=" and add_time<='".$endtime."'";
		}
		return $this->table->getAdapter()->fetchOne("select count(*) from order_log ".$where);
	}
}